#include "UIPause.h"
#include "Common.h"
#include "GameScene.h"
#include "GameLayer.h"

UIPause::UIPause()
{

}

UIPause::~UIPause()
{

}

UIPause* UIPause::create()
{
	UIPause* p = new UIPause();
	if (p && p->init())
	{
		p->autorelease();
		return p;
	}

	CC_SAFE_DELETE(p);
	return NULL;
}

bool UIPause::init()
{
	CCLayer::init();
	bool bRet = false;
	do 
	{
		UILayer* pUILayer = UILayer::create();
		CC_BREAK_IF(!pUILayer);

		UILayout* pRoot = (UILayout*)GUIReader::shareReader()->widgetFromJsonFile("ui/pause/pause_1.json");
		pRoot->setSize(getContentSize());
		pUILayer->addWidget(pRoot);
		addChild(pUILayer);

		{
			UIButton* pBtn = dynamic_cast<UIButton*>(UIHelper::seekWidgetByTag(pRoot, EUIPauseTag_menu));
			CC_BREAK_IF(!pBtn);
			pBtn->addTouchEventListener(this, toucheventselector(UIPause::onMenu));
		}

		{
			UIButton* pBtn = dynamic_cast<UIButton*>(UIHelper::seekWidgetByTag(pRoot, EUIPauseTag_play));
			CC_BREAK_IF(!pBtn);
			pBtn->addTouchEventListener(this, toucheventselector(UIPause::onPlay));
		}

		{
			UIButton* pBtn = dynamic_cast<UIButton*>(UIHelper::seekWidgetByTag(pRoot, EUIPauseTag_retry));
			CC_BREAK_IF(!pBtn);
			pBtn->addTouchEventListener(this, toucheventselector(UIPause::onRetry));
		}


		bRet = true;
	} while (0);
	return bRet;
}

void UIPause::onPlay(CCObject* pSender, TouchEventType eType)
{
	checkTouchEventTypeIsEnd(eType);
	GameScene* pScene = (GameScene*)getParent();
	pScene->getGameLayer()->resumeSchedulerAndActions();
	removeFromParent();
}

void UIPause::onMenu(CCObject* pSender, TouchEventType eType)
{
	checkTouchEventTypeIsEnd(eType);

}

void UIPause::onRetry(CCObject* pSender, TouchEventType eType)
{
	checkTouchEventTypeIsEnd(eType);
	CCDirector::sharedDirector()->replaceScene( CCTransitionProgressOutIn::create(1, GameScene::create()));
}

void UIPause::onEnter()
{
	CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this, 0, true);
	CCLayer::onEnter();
}

void UIPause::onExit()
{
	CCDirector::sharedDirector()->getTouchDispatcher()->removeDelegate(this);
	CCLayer::onExit();
}
